package com.zhou.framework.quartz.common;

import com.zhou.util.Assert;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Properties;

/**
 * @author lang.zhou
 * @date 2019/12/20
 */
public class SchedulerFactory {
    public static String defaultConnectionName = "quartzConnection";
    public static Properties getDefaultProperties(){
        Properties property = new Properties();
        property.setProperty("org.quartz.threadPool.threadCount","15");
        property.setProperty("com.zhou.quartz.connectionName",defaultConnectionName);
        property.setProperty("org.quartz.scheduler.instanceName","MyScheduler");
        property.setProperty("org.quartz.jobStore.misfireThreshold","1");
        property.setProperty("org.quartz.threadPool.class","org.quartz.simpl.SimpleThreadPool");
        property.setProperty("org.quartz.scheduler.instanceId","AUTO");
        property.setProperty("org.quartz.threadPool.threadPriority","5");
        property.setProperty("com.zhou.quartz.listener","com.zhou.base.quartz.listener.MyTriggerListener");
        return property;
    }
    public static Scheduler createScheduler() {
        try {
            StdSchedulerFactory factory = new StdSchedulerFactory();
            Properties property = getDefaultProperties();

            factory.initialize(property);

            return factory.getScheduler();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public static Scheduler createScheduler(Properties property) {
        try {
            StdSchedulerFactory factory = new StdSchedulerFactory();
            Assert.notNull(property," quartz property cant be null ");
            factory.initialize(property);
            String connectionName = property.getProperty("com.zhou.quartz.connectionName","");

            if(connectionName.length() > 0){
                defaultConnectionName = connectionName;
            }
            return factory.getScheduler();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
